package com.my.screenadapter.bundle;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;

import com.my.screenadapter.R;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Set;
import java.lang.Exception;


public class DoBundleActivity extends AppCompatActivity {
    private static final String TAG = "DoBundleActivity";
    private Bundle bundle;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_do_bundle);


        bundle = getIntent().getExtras();
        saveBundleToFile(bundle);

    }

    public static void saveBundleToPropert(Bundle bundle) {
        Log.i(TAG, "saveBundleToPropert: ");
        if (bundle == null) {
            return;
        }
        Set<String> keySet = bundle.keySet();
        for (String key : keySet){
            Object o = bundle.get(key);
            Log.i(TAG, "saveBundleToPropert: object--- key:"+key+"  v:"+o);
            try {
//                import android.os.SystemProperties;
//            SystemProperties.set(key,(String)o);
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }

    private void saveBundleToFile(Bundle bundle) {
        if (bundle == null) {
            return;
        }
        Set<String> keySet = bundle.keySet();

        File file = new File("/sdcard/beeboxes/saveconfig.txt");
//        String path = this.getExternalCacheDir().getAbsolutePath()+ File.separator+"temp"+File.separator+"saveconfig.txt";
//        File file = new File(path);
        Log.i(TAG, "onCreate: filePath: "+file.getAbsolutePath());
        if (file.exists()){
            file.delete();
        }else {
//            boolean mkdirs = file.mkdirs();//(Is a directory)
            boolean mkdirs = new File(file.getParent()).mkdirs();//(Is a directory)
            Log.i(TAG, "onCreate: mkdirs: "+mkdirs);
        }
        for (String key : keySet){
            Object o = bundle.get(key);
            Log.i(TAG, "onCreate: object--- key:"+key+"  v:"+o);
            writeStrToFile(key+":"+String.valueOf(o),file);
        }
    }

    /**
     * 追加文件：使用FileWriter
     *
     * @param fileName
     * @param content
     */
    public static void appendFileWriter(String fileName, String content) {
        FileWriter writer = null;
        try {
            // 打开一个写文件器，构造函数中的第二个参数true表示以追加形式写文件
            writer = new FileWriter(fileName, true);
            writer.write(content);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if(writer != null){
                    writer.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    private void writeStrToFile(String s, File file) {
        appendFileWriter(file.getAbsolutePath(),s);
        appendFileWriter(file.getAbsolutePath(),"\r\n");
    }

}